clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\cand_level_persist_rep.dta",clear

keep muni_code margin_victory fdonate_15any fb5

foreach var in donate_15any b5{
    rename  f`var' `var'
}

gen family=1

save  "Data\cand_level_persist_fam_aux_rep.dta",replace

use  "Data\cand_level_persist_rep.dta",clear

keep muni_code margin_victory nfdonate_15any nfb5

foreach var in donate_15any b5{
    rename  nf`var' `var'
}

gen family=0

append using "Data\cand_level_persist_fam_aux_rep.dta"


		
	gen treat=0
	replace treat=1 if margin_victory>0&margin_victory~=.
	replace treat=. if margin_victory==.

	gen treat_margin_victory=treat*margin_victory
	gen margin_victory_sq=margin_victory^2
	gen treat_margin_victory_sq=treat*margin_victory_sq


	texdoc close 
	cap erase "$dir/Tables/TableF5.tex"
	texdoc init "$dir/Tables/TableF5.tex", force
	
	tex \begin{table}[tbph]
	tex \caption{Effect of donating to an election winner on future donations (candidate's family members vs. Non members global parametric quadratic RD)}\label{tab:donation_fam_nofam_inter_q}
	tex \centering
	tex \begin{tabular}{l c c } \hline
	tex Outcome : & Any race & Mayor  \\ 
	tex & (1) & (2)  \\ \hline
	tex & &  \\
	
	*Model 1
	foreach x in donate_15any b5{
			
		quietly: regress `x' (i.treat c.treat_margin_victory_sq c.margin_victory_sq c.treat_margin_victory c.margin_victory)##i.family, vce(cluster muni_code)
		quietly sum `x' if e(sample)
			local fmean_`x' : di %5.3f r(mean)
			local fsd_`x' : di %5.3f r(sd) 		
		
		regress `x' (i.treat c.treat_margin_victory_sq c.margin_victory_sq c.treat_margin_victory c.margin_victory)##i.family, vce(cluster muni_code)

		local fN_`x' : di %5.0f e(N)
		local fR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local fb1_`x' : di %5.3f b[1,2]
		local fse1_`x' : di %5.3f sqrt(v[1,2])
		local fp_v1_`x' :di %5.3f res[4,2]
		local fuci1_`x': di %5.3f res[6,2]
		local flci1_`x': di %5.3f res[5,2]
		
		local fb2_`x' : di %5.3f b[1,12]
		local fse2_`x' : di %5.3f sqrt(v[1,12])
		local fp_v2_`x' :di %5.3f res[4,12]
		local fuci2_`x': di %5.3f res[6,12]
		local flci2_`x': di %5.3f res[5,12]

		lincom 1.treat+ 1.treat#1.family
		
		local fb3_`x': di %5.3f r(estimate)
		local fp_v3_`x' :di %5.3f r(p)
		local fuci3_`x': di %5.3f r(ub)
		local flci3_`x': di %5.3f r(lb)
		
	}
	
	*Continue table
	tex Electoral victory & `fb1_donate_15any' & `fb1_b5'  \\
	tex \ \ \ \ Robust p-value & `fp_v1_donate_15any' & `fp_v1_b5' \\
	tex \ \ \ \ CI 95\%  & [`flci1_donate_15any',`fuci1_donate_15any'] & [`flci1_b5',`fuci1_b5']  \\
	tex & &  \\
	
	tex Electoral victory $\times$ Family & `fb2_donate_15any' & `fb2_b5'  \\
	tex \ \ \ \ Robust p-value & `fp_v2_donate_15any' & `fp_v2_b5' \\
	tex \ \ \ \ CI 95\%  & [`flci2_donate_15any',`fuci2_donate_15any'] & [`flci2_b5',`fuci2_b5']  \\
	tex & &  \\ \hline
	
	tex Electoral victory (Family) & `fb3_donate_15any' & `fb3_b5'  \\
	tex \ \ \ \ Robust p-value & `fp_v3_donate_15any' & `fp_v3_b5' \\
	tex \ \ \ \ CI 95\%  & [`flci3_donate_15any',`fuci3_donate_15any'] & [`flci3_b5',`fuci3_b5']  \\
	tex & &  \\ \hline
	
	tex Observations & `fN_donate_15any' & `fN_b5' \\
	tex Mean & `fmean_donate_15any' & `fmean_b5' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex \footnotesize{OLS estimates of average treatment effects at the cutoff for family and non- family donors. Controls include the interaction of the treatment with running variable and running variable, interaction of the treatment with the squared running variable, the running variable squared, interaction of family dummy with all previous variables, and family dummy. P-values and 95\% robust confidence intervals with clustering at the municipality level.  
	tex }
	tex }}
	tex \end{table}
	cap texdoc close 